package org.dromara.wmscloud.controller;

import java.util.ArrayList;
import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.common.excel.core.ExcelResult;
import org.dromara.wmscloud.domain.vo.BaseMatBomImportVo;
import org.dromara.wmscloud.listener.BaseMatBomImportListener;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.wmscloud.domain.vo.BaseMatBomVo;
import org.dromara.wmscloud.domain.bo.BaseMatBomBo;
import org.dromara.wmscloud.service.IBaseMatBomService;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 物料BOM
 *
 * @author hou
 * @date 2025-04-05
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/wmsCloud/matBom")
public class BaseMatBomController extends BaseController {

    private final IBaseMatBomService baseMatBomService;

    /**
     * 查询物料BOM列表
     */
    @SaCheckPermission("base:bom:list")
    @GetMapping("/list")
    public TableDataInfo<BaseMatBomVo> list(BaseMatBomBo bo, PageQuery pageQuery) {
        return baseMatBomService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出物料BOM列表
     */
    @SaCheckPermission("base:bom:export")
    @Log(title = "物料BOM", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(BaseMatBomBo bo, HttpServletResponse response) {
        List<BaseMatBomVo> list = baseMatBomService.queryList(bo);
        ExcelUtil.exportExcel(list, "物料BOM", BaseMatBomVo.class, response);
    }

    /**
     * 获取物料BOM详细信息
     *
     * @param bomId 主键
     */
    @SaCheckPermission("base:bom:query")
    @GetMapping("/{bomId}")
    public R<BaseMatBomVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long bomId) {
        return R.ok(baseMatBomService.queryById(bomId));
    }

    /**
     * 新增物料BOM
     */
    @SaCheckPermission("base:bom:add")
    @Log(title = "物料BOM", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody BaseMatBomBo bo) {
        return toAjax(baseMatBomService.insertByBo(bo));
    }

    /**
     * 修改物料BOM
     */
    @SaCheckPermission("base:bom:edit")
    @Log(title = "物料BOM", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody BaseMatBomBo bo) {
        return toAjax(baseMatBomService.updateByBo(bo));
    }

    /**
     * 删除物料BOM
     *
     * @param bomIds 主键串
     */
    @SaCheckPermission("base:bom:remove")
    @Log(title = "物料BOM", businessType = BusinessType.DELETE)
    @DeleteMapping("/{bomIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] bomIds) {
        return toAjax(baseMatBomService.deleteWithValidByIds(List.of(bomIds), true));
    }

    /**
     * 获取导入模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil.exportExcel(new ArrayList<>(), "用户数据", BaseMatBomImportVo.class, response);
    }

    /**
     * 导入数据
     *
     * @param file          导入文件
     */
    @Log(title = "物料BOM", businessType = BusinessType.IMPORT)
    @SaCheckPermission("base:bom:add")
    @PostMapping(value = "/importData", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<Void> importData(@RequestPart("file") MultipartFile file) throws Exception {
        ExcelResult<BaseMatBomImportVo> result = ExcelUtil.importExcel(file.getInputStream(), BaseMatBomImportVo.class, new BaseMatBomImportListener());
        return R.ok(result.getAnalysis());
    }
}
